<?php
/*
 * motif/load-index.phtml
 */
?>
<?php
$motifs = $this->motifs;
?>
<div class="list-container">
    <table class="table table-striped list-table" border="1">
	<thead>
	    <tr>
		<td width="45%">Libellé</td>
                <td width="45%">Commentaire</td>
		<td width="10%">&nbsp;</td>
	    </tr>
	</thead>
	<tbody>
	    <?php foreach ($motifs as $m): ?>
    	    <tr>
    		<td>
		    <a href="#" class="edit-libelle" data-type="text" data-pk="<?php echo $m->id; ?>" data-name="libelle"><?php echo $m->libelle; ?></a>
    		</td>
                    <td>
			<a href="#" class="edit-commentaire" data-type="text" data-pk="<?php echo $m->id; ?>" data-name="commentaire"><?php echo $m->commentaire; ?></a>
    		    </td>
    		<td>
		    <a href='#' class='delete-x' data-libelle="<?php echo $m->libelle; ?>" data-id='<?php echo $m->id; ?>'>
    			<img src='<?php echo $this->basePath() . '/img/rhx/delete-16x16.png' ?>' alt='Supprimer' title='Supprimer'>
    		    </a>
                    </td>
    	    </tr>
	    <?php endforeach; ?>
	</tbody>
    </table>
</div>

<script type="text/javascript">
// Démarrage
    $(function() {
	// Edition
	$('.edit-libelle').editable({
	    title: "Edition du libellé",
	    url: '<?php echo $this->url('RHX/default', array('controller'	 => 'motif', 'action'	 => 'edit')); ?>'
	});
	$('.edit-commentaire').editable({
	    title: "Edition du commentaire",
	    url: '<?php echo $this->url('RHX/default', array('controller'	 => 'motif', 'action'	 => 'edit')); ?>'
	});
	$('.delete-x').click(function(e){
	    e.preventDefault();
	    id = $(this).attr('data-id');
	    libelle = $(this).attr('data-libelle');
	    if (!confirm("Voulez-vous vraiment effacer le motif " + libelle + " ?")){
		return false;
	    }
	    var urlDelete = "<?php echo $this->url('RHX/default', array('controller'	 => 'motif', 'action'	 => 'erase')) ?>";
	    var ajax = $.ajax({
		type: 'post',
		url: urlDelete,
		data: '&id=' + id
	    });
	    ajax.success(function(result) {
		if(!result.done){
		    alert("Une erreur est survenue lors de la suppression du motif !");
		}
		refresh();
	    });
	    ajax.error(function() {
		alert('Une erreur est survenue');
	    });
	    return false;
	});
    });
</script>

